package com.chunuo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.eduservice.entity.EduTeacher;
import com.chunuo.eduservice.entity.vo.TeacherQuery;
import com.chunuo.eduservice.mapper.EduTeacherMapper;
import com.chunuo.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public IPage<EduTeacher> pageList(Integer page, Integer limit, TeacherQuery teacherQuery) {
        Page<EduTeacher> pageList = new Page<>(page,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }

        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }

        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //根据更新时间倒序
        wrapper.orderByDesc("gmt_modified");

        IPage<EduTeacher> teacherIPage = this.page(pageList, wrapper);
        return teacherIPage;
    }

    /**
     * 前台的热门推荐
     * @return
     */
    @Cacheable(value = "teacher",key = "'popularTeacher'")
    @Override
    public List<EduTeacher> popularTeacher() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper();
        wrapper.orderByDesc("id");
        wrapper.last("limit 4");
        List<EduTeacher> teacherList = baseMapper.selectList(wrapper);

        return teacherList;
    }

    /**
     * 讲师分页
     * @param pageParam
     * @return
     */
    @Override
    public Map pageQeryTeacher(Page<EduTeacher> pageParam) {
        QueryWrapper wrapper = new QueryWrapper();
        baseMapper.selectPage(pageParam, wrapper);

        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
